/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.packetclient;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.lineage.server.Account;
import com.lineage.server.ClientThread;
import com.lineage.server.model.Instance.L1PcInstance;

/**
 * 处理收到由客户端传来断线的封包
 */
public class C_Disconnect extends ClientBasePacket {

    private static final String C_DISCONNECT = "[C] C_Disconnect";

    private static Logger _log = Logger.getLogger(C_Disconnect.class.getName());

    public C_Disconnect(final byte[] decrypt, final ClientThread client) {
        super(decrypt);
        client.charReStart(true);
        final L1PcInstance pc = client.getActiveChar();
        try {
            if (pc != null) {
                _log.info("退出游戏(角色) : " + pc.getName());

                if (client.getAccount() != null) {
                    Account.online(client.getAccount(), false);
                }

                ClientThread.quitGame(pc);

                synchronized (pc) {
                    pc.logout();
                    client.setActiveChar(null);
                }
            } else {
                _log.info("退出游戏(账号) : " + client.getAccountName());
            }
        } catch (final Exception e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        }
    }

    @Override
    public String getType() {
        return C_DISCONNECT;
    }
}
